<script setup lang="ts" name="DataInputOutputMapAddEdit">
import { errorMessage } from '@/utils/message'
const AddEditFormRef = ref(null)

const emit = defineEmits(['save'])

const props = defineProps({
  data: {
    type: Array,
    default: () => {
      return []
    }
  }
})

const visible = ref(false)
const title = ref('')
const formData = ref({
  key: '',
  value: ''
})
const editIndex = ref(-1)
const rules = ref({
  key: { required: true, message: '键名不能为空', trigger: ['blur', 'change'] },
  value: { required: true, message: '值不能为空', trigger: ['blur', 'change'] }
})
// 显示新建/编辑部门弹窗
const showDialog = (v) => {
  visible.value = true
  editIndex.value = v.index
  if (v.row) {
    title.value = '编辑映射条目'
    formData.value.key = v.row.key
    formData.value.value = v.row.value
  } else {
    title.value = '添加映射条目'
  }
}
// 子组件将方法暴露给父组件
defineExpose({ showDialog })
const closeDialog = () => {
  formData.value.key = ''
  formData.value.value = ''
  if (AddEditFormRef.value) {
    AddEditFormRef.value.clearValidate()
  }
  visible.value = false
}

const isMapKeyValidate = (data) => {
  let isKeySame = false
  data.forEach((item, index) => {
    if (item.key === formData.value.key && (editIndex.value === -1 || editIndex.value !== -1 && editIndex.value !== index)) {
      isKeySame = true
    }
  })
  return isKeySame
}

const isMapValueValidate = (data) => {
  let isValueSame = false
  data.forEach((item, index) => {
    if (item.value === formData.value.value && (editIndex.value === -1 || editIndex.value !== -1 && editIndex.value !== index)) {
      isValueSame = true
    }
  })
  return isValueSame
}

const handleOk = () => {
  if (!AddEditFormRef.value) return false
  AddEditFormRef.value.validate((valid) => {
    if (valid) {
      console.log(props.data, '映射条目数据')
      if (isMapKeyValidate(props.data)) {
        errorMessage('映射条目的键名必须唯一！')
        return false
      }
      if (isMapValueValidate(props.data)) {
        errorMessage('映射条目的值必须唯一！')
        return false
      }
      const result: any = {}
      result.key = formData.value.key
      result.value = formData.value.value
      emit('save', result)
      closeDialog()
    }
  })
}
</script>

<template>
  <el-dialog v-model="visible" width="600px" append-to-body destroy-on-close :title="title">
    <el-form ref="AddEditFormRef" label-width="auto" label-position="right" :model="formData" :rules="rules">
      <el-form-item label="键名" prop="key">
        <el-input v-model="formData.key" clearable placeholder="请输入键名" />
      </el-form-item>
      <el-form-item label="值" prop="value">
        <el-input v-model="formData.value" clearable placeholder="请输入值" />
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="modal-footer" ref="modalFooter">
        <el-button @click="closeDialog">{{ $t('buttons.buttonCancel') }}</el-button>
        <el-button type="primary" @click="handleOk">{{ $t('buttons.buttonConfirm') }}</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<style scoped lang="scss">
.el-form {
  padding: 40px 80px;
}

.modal-footer{
  display: inline-flex;
  overflow: hidden;
}
</style>
